from django.db import models
from config.choices import GENDER_CHOICES, USER_TYPE
from system.models import Role
from utils.models import CoreModel


# Create your models here.
class Users(CoreModel):
    username = models.CharField(max_length=150, unique=True, db_index=True,
                                verbose_name='用户账号', help_text="用户账号")
    password = models.CharField(max_length=255, verbose_name="密码", help_text="密码")
    password_str = models.CharField(max_length=255, verbose_name="明文密码", help_text="明文密码")
    email = models.EmailField(max_length=255, null=True, blank=True, verbose_name="邮箱", help_text="邮箱")
    mobile = models.CharField(max_length=255, null=True, blank=True, verbose_name="电话", help_text="电话")
    avatar = models.TextField(null=True, blank=True, verbose_name="头像", help_text="头像")
    name = models.CharField(max_length=40, verbose_name="姓名", help_text="姓名")
    gender = models.IntegerField(choices=GENDER_CHOICES, default=1, null=True, blank=True,
                                 verbose_name="性别", help_text="性别")
    user_type = models.IntegerField(choices=USER_TYPE, default=0, null=True, blank=True,
                                    verbose_name="用户类型", help_text="用户类型")

    class Meta:
        db_table = "auths_users"
        verbose_name = '用户表'
        verbose_name_plural = verbose_name
        ordering = ('-create_time',)


class UsersToken(CoreModel):
    username = models.CharField(max_length=150, verbose_name='用户账号', help_text="用户账号")
    user_token = models.CharField(max_length=888, verbose_name="用户身份token", help_text="用户身份token")
    exceed_time = models.CharField(max_length=50, verbose_name="过期时间", help_text="过期时间")

    class Meta:
        db_table = "auths_users_token"
        verbose_name = '用户token表'
        verbose_name_plural = verbose_name
        ordering = ('-create_time',)


class UsersRole(CoreModel):
    user = models.ForeignKey(to=Users, on_delete=models.CASCADE, verbose_name="关联用户", help_text="关联用户")
    role = models.ForeignKey(to=Role, on_delete=models.CASCADE, verbose_name="关联角色", help_text="关联角色")

    class Meta:
        db_table = "auths_users_role"
        verbose_name = '用户角色关联表'
        verbose_name_plural = verbose_name
        ordering = ('-create_time',)
